﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DiveBaseDataBaseLibrary;

namespace DiveManager
{
    class RightsProvider
    {
        protected DiveDatabaseEntities diveDatabase;

        public RightsProvider()
        {
            diveDatabase = new DiveDatabaseEntities();
        }

        public void rechteabgleich()
        {

            List<RechteGruppe> alleRechte = new List<RechteGruppe>();
            alleRechte.Add(new RechteGruppe("Benutzer", new List<string> { "Anzeigen", "Anlegen", "Bearbeiten", "Löschen" }));
            alleRechte.Add(new RechteGruppe("Kunde", new List<string> { "Anzeigen", "Anlegen", "Bearbeiten", "Löschen" }));

            List<TypeGruppenRechte> query = getAlleGruppenUndRechte();

            SecurityGroups addGruppe;
            Guid gruppenID;
            foreach (RechteGruppe gruppe in alleRechte)
            {
                //generell die Gruppe prüfen
                int i = (from q in query
                      where q.GroupName == gruppe.Name
                     select q).Count();
                if (i == 0)
                {
                    addGruppe = new SecurityGroups();
                    gruppenID = Guid.NewGuid();
                    addGruppe.ID = gruppenID;
                    addGruppe.name = gruppe.Name;
                    diveDatabase.AddToSecurityGroups(addGruppe);
                    diveDatabase.SaveChanges();
                }
                else
                {
                    gruppenID = (from q in query
                                 where q.GroupName == gruppe.Name
                                 select q.GroupID).First();
                }

                foreach (string recht in gruppe.Rechteliste)
                {
                    query = getAlleGruppenUndRechte();
                    var j = (from q in query
                             where q.GroupName == gruppe.Name && q.Rightname == recht
                             select q).Count();
                    if (j == 0)
                    {
                        
                        RelationSecuritygroupsSecurityrights addRelation = new RelationSecuritygroupsSecurityrights();
                        SecurityRights addRights = new SecurityRights();
                        
                        Guid rechteID = Guid.NewGuid();
                        
                        addRights.ID = rechteID;
                        addRights.right = recht;

                        addRelation.ID = Guid.NewGuid();
                        addRelation.groupID = gruppenID;
                        addRelation.rightID = rechteID;

                        diveDatabase.AddToSecurityRights(addRights);
                        diveDatabase.AddToRelationSecuritygroupsSecurityrights(addRelation);                                                
                    }
                }                
            }
            diveDatabase.SaveChanges();
        }

        private List<TypeGruppenRechte> getAlleGruppenUndRechte()
        {
            List<TypeGruppenRechte> query = (from groups in diveDatabase.SecurityGroups
                                                       join
                                                           relation in diveDatabase.RelationSecuritygroupsSecurityrights on groups.ID equals relation.groupID
                                                           into groupwithrelation
                                                       from gwr in groupwithrelation
                                                       join
                                                           sr in diveDatabase.SecurityRights on gwr.rightID equals sr.ID
                                                       select new TypeGruppenRechte
                                                       {
                                                           GroupID = groups.ID,
                                                           GroupName = groups.name,

                                                           RelationID = gwr.ID,
                                                           RelationgroupID = gwr.groupID,
                                                           RelationrightID = gwr.rightID,

                                                           RightID = sr.ID,
                                                           Rightname = sr.right,
                                                       }).ToList();
            return query;
        }
    }
}
